home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 August (Alt) / CHIP 2005-08.1.iso / program / guvenlik / syslinux-3.07.exe / menu / heap.c < prev    next >
Encoding:
C/C++ Source or Header  |  2004-05-17  |  1.4 KB  |  74 lines

  1. /*
  2.  * Very simple heap manager.
  3.  * Allocation Strategy: The lower end of the heap, is the end of the BSS.
  4.  *
  5.  * During an alloc, if there is enough space below the high end of heap, 
  6.  *    we return a pointer to the allocated space and move curr.
  7.  * Space once allocated is never deallocated!
  8.  * We run out of space if we get within BUFSIZE bytes of the stack pointer.
  9.  */
  10.  
  11. #include "heap.h"
  12. #include "string.h"
  13. #include "biosio.h"
  14.  
  15. #ifndef NULL
  16. #define NULL ((void *)0)
  17. #endif
  18.  
  19. extern char _end[];
  20. static unsigned int heap_curr  = (unsigned int)_end;
  21.  
  22. static inline unsigned int currsp(void)
  23. {
  24.     unsigned int esp;
  25.     
  26.     asm("movl %%esp,%0 " : "=rm" (esp));
  27.     return esp;
  28. }
  29.  
  30. static inline void _checkheap(void)
  31. {
  32.     if (currsp() < heap_curr) // Heap corrupted
  33.     {
  34.     csprint("\r\nHeap overflow, aborting!\r\n",0x07);
  35.     asm volatile("int $0x21" : : "a" (0x4C7f)); /* Exit with error */
  36.     return;
  37.     }
  38. }
  39.  
  40. void * malloc(unsigned int num) // Allocate so much space
  41. {
  42.     unsigned int ans, heap_max;
  43.  
  44.     _checkheap();
  45.     heap_max = currsp() - STACKSIZE;
  46.  
  47.     ans = (heap_curr+3) & ~3;    // Align to 4-byte boundary
  48.  
  49.     if ( ans+num > heap_max )
  50.     return NULL;
  51.  
  52.     heap_curr = ans+num;
  53.     return (void *) ans;
  54. }
  55.  
  56. /* We don't actually ever use these; if enabled,
  57.    probably _checkheap() shouldn't be inline.*/
  58. #if 0
  59.  
  60. int checkalloc(unsigned int num)
  61. {
  62.     _checkheap();
  63.     return (heap_curr + num < heap_max);
  64. }
  65.  
  66.  
  67. void free(void * ptr)
  68. {
  69.     _checkheap();
  70.     return;
  71. }
  72.  
  73. #endif
  74.